package Listeners;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import GUI.ConferenceWizardFrame;
import GUI.ProgramChairPanel;
import Model.Conference;
import Model.Paper;
import Model.User;

/**
 * A listener that assigns a paper to a subprogram chair in a conference.
 * 
 * @author InnoIrving Estrera
 * */
@SuppressWarnings("serial")
public class AssignSubchairListener extends AbstractAction
{
  private Conference my_conference;
  private Paper my_paper;
  private User my_logged_in;
  private ConferenceWizardFrame my_updated_frame;

  public AssignSubchairListener(final Conference the_conference,
      final Paper the_paper, final User the_logged_in_user,
      final ConferenceWizardFrame the_frame)
  {
    my_conference = the_conference;
    my_paper = the_paper;
    my_logged_in = the_logged_in_user;
    my_updated_frame = the_frame;
  }

  /**
   * Will pop up a selection for all available subprogram chairs.
   * */
  @Override
  public void actionPerformed(ActionEvent arg0)
  {
    // list that holds all available sub program chairs.
    // It excludes sub program chairs
    // who have been assigned more than 4 papers.
    List<String> available_subchairs = new ArrayList<String>();
    Map<String, User> string_to_user = new HashMap<String, User>();
    for (final User a_sub_chair : my_conference.subProgramChairs())
    {
      if (!my_paper.getAuthor().equals(a_sub_chair) && my_conference.getPapersAssignedToSubChairs(a_sub_chair).size() < 4)
      {
        available_subchairs.add(a_sub_chair.realName());
        string_to_user.put(a_sub_chair.realName(), a_sub_chair);
      }
    }

    final String selected_subchair = (String) JOptionPane.showInputDialog(null,
        "Select a subprogram chair for this paper.", "Assign",
        JOptionPane.PLAIN_MESSAGE, null, available_subchairs.toArray(), null);
    if (selected_subchair != null) {
      my_paper.setSubprogramChair(string_to_user.get(selected_subchair));
      my_updated_frame.setCurrentPanel(new ProgramChairPanel(my_conference,
          my_logged_in, my_updated_frame));
    }
  }
}
